package com.ludaxu.zhbj.base.menudetail;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.transition.Slide;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.ludaxu.zhbj.MainActivity;
import com.ludaxu.zhbj.R;
import com.ludaxu.zhbj.base.BaseMenuDetailPager;
import com.ludaxu.zhbj.base.TabDetailPager;
import com.ludaxu.zhbj.domain.NewsData.NewsTabsData;
import com.viewpagerindicator.TabPageIndicator;

/**
 * 菜单详情页-新闻
 * 
 * @author ludaxu
 * 
 */
public class NewsMenuDetailPager extends BaseMenuDetailPager implements
		OnPageChangeListener {
	private ViewPager mViewPager;
	private ArrayList<TabDetailPager> mPagerList;
	private ArrayList<NewsTabsData> mNewsTabsDatas;// 页签网络数据
	private TabPageIndicator mIndicator;

	/**
	 * 实现父类的构造方法
	 * 
	 * @param activity
	 * @param children
	 */
	public NewsMenuDetailPager(Activity activity,
			ArrayList<NewsTabsData> children) {
		super(activity);
		mNewsTabsDatas = children;
	}

	@Override
	public View initViews() {
		View view = View.inflate(mActivity, R.layout.news_menu_detail, null);
		mViewPager = (ViewPager) view.findViewById(R.id.vp_menu_detail);

		ViewUtils.inject(this, view);

		// 初始化自定义控件TabPageIndicator
		mIndicator = (TabPageIndicator) view.findViewById(R.id.indicator);
		// 当viewpager和indicator绑定时,需要设置滑动监听时,需要设置给indicator
		mIndicator.setOnPageChangeListener(this);
		return view;
	}

	@Override
	public void initData() {
		mPagerList = new ArrayList<TabDetailPager>();
		// 初始化页签数据
		for (int i = 0; i < mNewsTabsDatas.size(); i++) {
			TabDetailPager pager = new TabDetailPager(mActivity,
					mNewsTabsDatas.get(i));
			mPagerList.add(pager);
		}
		mViewPager.setAdapter(new MenuDetailAdapter());
		// 必须在viewpager设置完adapter后才能调用,将indicator和viewpager关联起来
		mIndicator.setViewPager(mViewPager);
	}

	// xutils ,,,跳转到下一个页面
	@OnClick(R.id.bt_next)
	public void nextPager(View view) {

		int currentItem = mViewPager.getCurrentItem();
		// 不需要判断角标越界的问题,ViewPager已经帮处理了
		mViewPager.setCurrentItem(++currentItem);
	}

	class MenuDetailAdapter extends PagerAdapter {
		// TabPageIndicator 返回页面标题,用于页签的显示
		@Override
		public CharSequence getPageTitle(int position) {
			return mNewsTabsDatas.get(position).title;
		}

		@Override
		public int getCount() {
			return mPagerList.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			TabDetailPager pager = mPagerList.get(position);
			container.addView(pager.mRootView);
			pager.initData();

			return pager.mRootView;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int arg0) {
		System.out.println("onPageSelected:" + arg0);
		MainActivity mainUi = (MainActivity) mActivity;
		SlidingMenu slidingMenu = mainUi.getSlidingMenu();
		if (arg0 == 0) {
			slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		} else {
			slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
		}
	}

}
